package com.kefu.chat.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.kefu.chat.mapper.ChatFaqGroupMapper;
import com.kefu.chat.model.ChatFaqGroup;
import com.kefu.chat.vo.ChatFaqGroupSearchVo;
import com.kefu.common.context.KefuContext;
import com.kefu.common.service.AbstractKefuService;
import com.kefu.common.util.Assert;
import com.kefu.robot.model.RobotDocType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ChatFaqGroupService extends AbstractKefuService<ChatFaqGroup> {
    @Resource
    private ChatFaqGroupMapper chatFaqGroupMapper;
    public List treeGroup(ChatFaqGroupSearchVo search) {
        List<ChatFaqGroup> chatFaqGroups = chatFaqGroupMapper.selectList(search.lambdaQuery());
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        for (ChatFaqGroup chatFaqGroup : chatFaqGroups) {
            nodeList.add((new TreeNode<>(chatFaqGroup.getId(), chatFaqGroup.getParentId(), chatFaqGroup.getFaqGroupName(), chatFaqGroup.getSortNo())));
        }
        return TreeUtil.build(nodeList, "0");
    }

    public boolean save(ChatFaqGroup chatFaqGroup){
        boolean exist=chatFaqGroupMapper.existGroupName(chatFaqGroup.getFaqGroupName(),chatFaqGroup.getTenantId(),null);
        Assert.isTrue(exist,"存在相同的问题库");

        return super.save(chatFaqGroup);
    }

    public ChatFaqGroup update(ChatFaqGroup chatFaqGroup){
        boolean exist=chatFaqGroupMapper.existGroupName(chatFaqGroup.getFaqGroupName(),chatFaqGroup.getTenantId(),chatFaqGroup.getId());
        Assert.isTrue(exist,"存在相同的问题库");

        return super.update(chatFaqGroup);
    }
}
